package org.colcon.web.model;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.colcon.web.model.framework.IContainer;

public class Container extends Item implements IContainer
{
	public String getType(){return "container";}
	
	private static final long serialVersionUID = 5883704263223287840L;

	protected static final String
	
		// arguments
		CHILDREN = "children";
	
	private Set<String> children;
	
	public Container()
	{
		this(new HashMap<String, String>());
	}
	
	public Container(Map<String, String> args)
	{
		this(args.get(PARENT), args.get(ID), args.get(CHILDREN));
	}
	
	public Container(String parent, String id, String children)
	{
		super(parent, id);
		
		this.children = new LinkedHashSet<String>();
		
		if(children != null)
			for(String str : children.split(" "))
				this.children.add(str);
	}
	
	public String toString()
	{
		String str = super.toString();
		
		if(children.isEmpty())
			return str;
		
		str += " -children {";
		for(String key : children)
			str += key + " ";
		
		str = str.trim();
		
		str += "}";
		
		return str;
	}
	
	public String getPath()
	{
		if(isRoot())
			return getId();
		
		return super.getPath();
	}
	
	public String[] getChildren()
	{
		return children.toArray(new String[0]);
	}

	public boolean hasChild(String id)
	{
		return children.contains(id);
	}
	
	public boolean addChild(String id)
	{
		return children.add(id);
	}
	
	public boolean removeChild(String id)
	{
		return children.remove(id);
	}
}
